Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Namespace BMLERP
	Friend Class frmINV500
		Inherits System.Windows.Forms.Form


		Dim NewTrans As Byte

		Private Sub cboTHCVN_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboTHCVN.ClickEvent
			If eventArgs.area = 2 Then
				txtTHCVN.Text = cboTHCVN.BoundText
				txtName.Text = cboTHCVN.CtlText
				cboTHCVN.Visible = False
				If txtTHREF.Visible Then txtTHREF.Focus()
			End If
		End Sub

		Private Sub cboTHITN_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboTHITN.ClickEvent
			Dim sql As Object
			Dim DelStatus As Object
			Dim FieldDel As Object
			If eventArgs.area = 2 Then
				txtTHITN.Text = cboTHITN.BoundText
				txtIMITD.Text = Find("IIM", "IMITD", " WHERE IMITN='" & cboTHITN.BoundText & "'")
				cboTHITN.Visible = False
				FieldDel = "LMRID"
				DelStatus = "LZ"
				sql = "Select LMLOT from ILM where " & FieldDel & " <>'" & DelStatus & "' AND LMITN='" & txtTHITN.Text & "' Group by LMLOT"
				Adodc7.ConnectionString = ConnectString
				Adodc7.ConnectionTimeout = 10
				Adodc7.CommandType = ADODB.CommandTypeEnum.adCmdText
				Adodc7.RecordSource = sql
				Adodc7.Refresh()
				txtTHLTN.Visible = Find("IIM", "IMLOT", " WHERE IMITN='" & cboTHITN.BoundText & "'")
				' txtTHLTN.Visible = Find("IIM", "IMLOT", " WHERE IMITN='" & cboTHITN.BoundText & "'")
				If txtTHLTN.Visible Then txtTHLTN.Focus()
				If txtTHREF.Visible Then 'Have PO
					Adodc6.RecordSource = "Select OLPON from POL Where OLITN='" & txtTHITN.Text & "' AND OLSTT <> '7' group by OLPON"
					Adodc6.Refresh()
				End If
			End If
		End Sub

		Private Sub cboTHLTN_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboTHLTN.ClickEvent
			If eventArgs.area = 2 Then
				txtTHLTN.Text = cboTHLTN.CtlText
				cboTHLTN.Visible = False
				If txtTHCVN.Visible Then txtTHCVN.Focus()
			End If
		End Sub

		Private Sub cboTHREF_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboTHREF.ClickEvent
			If eventArgs.area = 2 Then
				txtTHREF.Text = cboTHREF.CtlText
				cboTHREF.Visible = False
				txtTHQTY.Focus()
			End If
		End Sub

		Private Sub cboTHTTC_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboTHTTC.ClickEvent
			Dim DelStatus As Object
			Dim FieldDel As Object
			Dim RecITC As ADODB.Recordset
			If eventArgs.area = 2 Then
				txtTHTTC.Text = cboTHTTC.BoundText
				txtTCDSC.Text = cboTHTTC.CtlText

				txtTHCVN.Visible = True
				txtName.Visible = True
				RecITC = DBconn.Execute("Select * from ITC where TCTTC='" & txtTHTTC.Text & "'")
				Adodc4.ConnectionString = ConnectString
				Adodc4.ConnectionTimeout = 10
				Adodc4.CommandType = ADODB.CommandTypeEnum.adCmdText
				If RecITC.Fields("TCEFR").Value Then
					txtTHREF.Visible = True
					FieldDel = "VMRID"
					DelStatus = "VZ"
					Adodc4.RecordSource = "Select * from AVM where " & FieldDel & " <>'" & DelStatus & "'"
					Adodc4.Refresh()
					lbl(9).Text = "Vendor"
					cboTHCVN.CtlText = ""
					cboTHCVN.BoundColumn = "VMVDN"
					cboTHCVN.ListField = "VMNME"
					Adodc1.RecordSource = "SELECT ITH.*,AVM.VMNME As Name ,Users.Name as UserName, IIM.IMITD " & " FROM Users RIGHT OUTER JOIN ITH INNER JOIN " & " IIM ON ITH.THITN = IIM.IMITN LEFT OUTER JOIN " & " AVM ON ITH.THCVN = AVM.VMVDN LEFT OUTER JOIN " & " RCM ON ITH.THCVN = RCM.CMCNO ON " & " Users.UserName = ITH.THLUS" & " WHERE THDOC='" & txtTHDOC.Text & "' Order by THTID"
					Adodc1.Refresh()
				Else
					txtTHREF.Visible = False
					cboTHCVN.CtlText = ""
					If RecITC.Fields("TCEFI").Value Then
						FieldDel = "CMRID"
						DelStatus = "CZ"
						lbl(9).Text = "Customer"
						Adodc4.RecordSource = "Select * from RCM where " & FieldDel & " <>'" & DelStatus & "'"
						Adodc4.Refresh()
						cboTHCVN.BoundColumn = "CMCNO"
						cboTHCVN.ListField = "CMNME"
						Adodc1.RecordSource = "SELECT ITH.*,RCM.CMNME As Name ,Users.Name as UserName, IIM.IMITD " & " FROM Users RIGHT OUTER JOIN ITH INNER JOIN " & " IIM ON ITH.THITN = IIM.IMITN LEFT OUTER JOIN " & " AVM ON ITH.THCVN = AVM.VMVDN LEFT OUTER JOIN " & " RCM ON ITH.THCVN = RCM.CMCNO ON " & " Users.UserName = ITH.THLUS" & " WHERE THDOC='" & txtTHDOC.Text & "' Order by THTID"
						Adodc1.Refresh()
					Else
						'            cboTHCVN.Visible = False
						txtTHCVN.Visible = False
						txtName.Visible = False
					End If

				End If
				' Adodc4.Refresh
				cboTHTTC.Visible = False
				txtTHITN.Focus()
			End If
		End Sub

		Private Sub cboTHWHC_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboTHWHC.ClickEvent
			If eventArgs.area = 2 Then
				txtTHWHC.Text = cboTHWHC.BoundText
				txtWMDSC.Text = cboTHWHC.CtlText
				cboTHWHC.Visible = False
				txtTHTTC.Focus()

			End If
		End Sub

		Private Sub cmdAddnew_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAddNew.Click
			NewTrans = True
			ISADDING = True
			ISEDITING = True
			WriteToScreen()
			ButtonStatus()
			txtTHDOC.Focus()
		End Sub

		Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click
			NewTrans = False
			ISADDING = False
			ISEDITING = False
			WriteToScreen()
			ButtonStatus()
		End Sub

		Private Sub cmdexit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExit.Click
			Me.Close()
		End Sub
		Sub WriteToScreen()
			Dim DelStatus As Object
			Dim FieldDel As Object
			Dim StrCV As Object
			If NewTrans Then
				txtTHDOC.Text = ""
				txtTHWHC.Text = ""
				txtWMDSC.Text = ""
				txtTHDTE.Text = CStr(WorkingDate)
				txtTHTTC.Text = ""
				txtTCDSC.Text = ""
			End If

			Dim recITH As ADODB.Recordset
			If ISADDING Then
				txtTHITN.Text = ""
				txtIMITD.Text = ""
				txtTHCVN.Text = ""
				txtName.Text = ""
				txtTHTSQ.Text = CStr(0)
				txtTHLTN.Text = ""
				txtTHREF.Text = ""
				txtTHQTY.Text = CStr(0)
				txtTHWGT.Text = CStr(0)
				txtTHCST.Text = CStr(0)
				txtTHREM.Text = ""
				StrCV = ""

			Else
				If txtTHTID.Text <> "" Then
					recITH = DBconn.Execute("select * from ITH where THTID=" & txtTHTID.Text)
					With recITH
						txtTHTID.Text = .Fields("THTID").Value
						txtTHDOC.Text = .Fields("THDOC").Value
						txtTHWHC.Text = .Fields("THWHC").Value
						txtWMDSC.Text = Find("IWM", "WMDSC", "Where WMWHC='" & .Fields("THWHC").Value & "'")
						txtTHDTE.Text = .Fields("THDTE").Value
						txtTHTTC.Text = .Fields("THTTC").Value
						txtTCDSC.Text = Find("ITC", "TCDSC", "Where TCTTC='" & .Fields("THTTC").Value & "'")
						txtTHITN.Text = .Fields("THITN").Value
						txtIMITD.Text = Find("IIM", "IMITD", "Where IMITN='" & txtTHITN.Text & "'")
						txtTHCVN.Text = .Fields("THCVN").Value
						If IsDBNull(.Fields("THTSQ").Value) Then
							txtTHTSQ.Text = ""
						Else
							txtTHTSQ.Text = .Fields("THTSQ").Value
						End If
						txtTHLTN.Text = .Fields("THLTN").Value
						If IsDBNull(.Fields("THTSQ").Value) Then
							txtTHREF.Text = ""
						Else
							txtTHREF.Text = .Fields("THREF").Value
						End If

						txtTHQTY.Text = .Fields("THQTY").Value
						txtTHWGT.Text = .Fields("THWGT").Value

						txtTHCST.Text = .Fields("THCST").Value
						If IsDBNull(.Fields("THTSQ").Value) Then
							txtTHREM.Text = ""
						Else
							txtTHREM.Text = .Fields("THREM").Value
						End If

					End With
				End If
			End If
			If txtTHDOC.Text <> "" Then
				If Find("ITC", "TCDSC", " Where TCTTC='" & txtTHTTC.Text & "' and TCEFR<>0") <> "" Then
					txtName.Text = Find("AVM", "VMNME", "where VMVDN='" & txtTHCVN.Text & "'")
					StrCV = ",AVM.VMNME As Name"
					FieldDel = "VMRID"
					DelStatus = "VZ"
					Adodc4.RecordSource = "Select * from AVM where " & FieldDel & " <>'" & DelStatus & "'"
					Adodc4.Refresh()
					cboTHCVN.BoundColumn = "VMVDN"
					cboTHCVN.ListField = "VMNME"
				Else
					txtName.Text = Find("RCM", "CMNME", "where CMCNO='" & txtTHCVN.Text & "'")
					StrCV = ",RCM.CMNME As Name"
					FieldDel = "CMRID"
					DelStatus = "CZ"
					Adodc4.RecordSource = "Select * from RCM where " & FieldDel & " <>'" & DelStatus & "'"
					Adodc4.Refresh()
					cboTHCVN.BoundColumn = "CMCNO"
					cboTHCVN.ListField = "CMNME"
				End If
			End If
			If Me.Adodc1.EOF Then
				Me.Adodc1 = New VB6.ADODC
				Adodc1.ConnectionString = ConnectString
				Adodc1.ConnectionTimeout = 10
				Adodc1.CommandType = ADODB.CommandTypeEnum.adCmdText
			End If

			Adodc1.RecordSource = "SELECT ITH.*" & StrCV & " ,Users.Name as UserName, IIM.IMITD " & " FROM Users RIGHT OUTER JOIN ITH INNER JOIN " & " IIM ON ITH.THITN = IIM.IMITN LEFT OUTER JOIN " & " AVM ON ITH.THCVN = AVM.VMVDN LEFT OUTER JOIN " & " RCM ON ITH.THCVN = RCM.CMCNO ON " & " Users.UserName = ITH.THLUS" & " WHERE THDOC='" & txtTHDOC.Text & "' and THTTC='" & txtTHTTC.Text & "' AND day(THDTE)=" & VB.Day(CDate(txtTHDTE.Text)) & " and month(THDTE)=" & Month(CDate(txtTHDTE.Text)) & " AND year(THDTE)=" & Year(CDate(txtTHDTE.Text)) & " Order by THTID"

			Adodc1.Refresh()
		End Sub

		Private Sub cmdNewItem_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdNewItem.Click
			'NewTrans = True
			ISADDING = True
			ISEDITING = True
			WriteToScreen()
			ButtonStatus()
		End Sub

		Private Sub cmdReview_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdReview.Click
			'Dim myform As New frmReview
			'myform.Tag = "INV500"
			frmReview.Tag = "INV500"
			frmReview.ShowDialog()
			ISEDITING = False
			ISADDING = False
			Dim recITH As ADODB.Recordset
			If frmReview.Tag <> "" Then
				txtTHDOC.Text = frmReview.GridList.Columns(1).Text
				txtTHDTE.Text = frmReview.GridList.Columns(2).Text
				txtTHTTC.Text = frmReview.GridList.Columns(4).Text
				frmReview.Close()
				'If myform.Tag <> "" Then
				'txtTHDOC.Text = myform.GridList.Columns(1).Text
				'txtTHDTE.Text = myform.GridList.Columns(2).Text
				'txtTHTTC.Text = myform.GridList.Columns(4).Text
				'myform.Close()
				recITH = DBconn.Execute("Select * from ITH where THDOC='" & txtTHDOC.Text & "' and THTTC='" & txtTHTTC.Text & "' AND day(THDTE)=" & VB.Day(CDate(txtTHDTE.Text)) & " and month(THDTE)=" & Month(CDate(txtTHDTE.Text)) & " AND year(THDTE)=" & Year(CDate(txtTHDTE.Text)))
				txtTHTID.Text = recITH.Fields("THTID").Value
				WriteToScreen()
			End If
		End Sub

		Private Sub cmdSave_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSave.Click
			If ValidInput() Then
				WriteToFile()
				NewTrans = False
				ISADDING = False
				ISEDITING = False
				WriteToScreen()
				ButtonStatus()
			End If
		End Sub

		Private Sub frmINV500_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
			ISADDING = False
			ISEDITING = False
			SELECTROW = 1
			OpenFile()
			VB6_AddADODataBinding()
			Me.cboTHWHC.RowSource = ADOBind_Adodc2.DataSource
			Me.cboTHTTC.RowSource = ADOBind_Adodc5.DataSource
			Me.cboTHITN.RowSource = ADOBind_Adodc3.DataSource
			Me.cboTHCVN.RowSource = ADOBind_Adodc4.DataSource
			'Me.cboTHLTN.RowSource = ADOBind_Adodc7.DataSource
			Me.cboTHREF.RowSource = ADOBind_Adodc6.DataSource
			Dim myDataCaptions As String() = {"ID", "Doc No", "Item", "Description", "Seq. No", "Lot.No", "Cust / Vendor", "Cust/Vendor Name", "Ref.No", "Qty", "WGT", "Value", "Remarks", "Staff name", "Update time"}
			Dim myDataFields As String() = {"THTID", "THDOC", "THITN", "IMITD", "THTSQ", "THLTN", "THCVN", "Name", "THREF", "THQTY", "THWGT", "THCST", "THREM", "UserName", "THLDT"}
			setDataGridView(myDataCaptions, myDataFields, Me.grdITH)

		End Sub

		Sub WriteToITH(ByRef WGT As Object, ByRef unit As Object)
			Dim sql As Object

			txtTHQTY.Text = IIf(IsNumeric(txtTHQTY.Text), txtTHQTY.Text, 0)
			txtTHWGT.Text = IIf(IsNumeric(txtTHWGT.Text), txtTHWGT.Text, 0)

			If Val(txtTHQTY.Text) = 0 And Val(txtTHWGT.Text) = 0 Then
				MsgBox("Quantity or Weight must be >0 ")
				Exit Sub
			End If

			'Lay so THTSQ
			txtTHTSQ.Text = GetTSQ(txtTHITN)
			Dim recITH As ADODB.Recordset
			recITH = DBconn.Execute("Select * from ITH Order by THTID DESC")
			If recITH.EOF Then
				txtTHTID.Text = CStr(1)
			Else
				txtTHTID.Text = recITH.Fields("THTID").Value + 1
			End If
			recITH.Close()

			txtTHDTE.Text = VB6.Format(txtTHDTE.Text, "yyyy-mm-dd hh:mm:ss")
			sql = "Insert into ITH (THITN,THTSQ,THTID,THDOC,THTTC,THDTE" & ",THWHC,THLTN,THQTY,THWGT,THCST" & ",THCVN,THREF,THREM,THLUS,THLDT) values " & "('" & txtTHITN.Text & "'," & txtTHTSQ.Text & "," & txtTHTID.Text & ",'" & txtTHDOC.Text & "','" & txtTHTTC.Text & "','" & txtTHDTE.Text & "'" & ",'" & txtTHWHC.Text & "','" & txtTHLTN.Text & "'," & unit & "," & WGT & "," & txtTHCST.Text & ",'" & txtTHCVN.Text & "'" & ",'" & txtTHREF.Text & "','" & txtTHREM.Text & "','" & UserLogin & "','" & VB6.Format(Now, "yyyy-mm-dd hh:mm:ss") & "')"
			' MsgBox SQL
			DBconn.Execute(sql)

			Adodc1.Refresh()
		End Sub
		Sub WriteToPOL(ByRef PON As Object, ByRef ITN As Object, ByRef WGT As Object, ByRef QTY As Object)

			DBconn.Execute("Update POL set OLRCQ= OLRCQ + " & QTY & ",OLRCW= OLRCW + " & WGT & " where OLPON='" & PON & "' AND OLITN='" & ITN & "' ")

		End Sub

		Private Sub grdITH_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles grdITH.ClickEvent
			txtTHDOC.Text = grdITH.Columns(2).Text
			txtTHTID.Text = grdITH.Columns(1).Text
			WriteToScreen()
			Adodc1.Recordset.AbsolutePosition = SELECTROW
			NewTrans = False
			ISADDING = False
			ISEDITING = False
			ButtonStatus()
		End Sub

		Private Sub grdITH_RowColChange(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataGridLib.DDataGridEvents_RowColChangeEvent) Handles grdITH.RowColChange
			SELECTROW = Adodc1.Recordset.Bookmark
			If SELECTROW = "" Then SELECTROW = 1
		End Sub

		Private Sub txtTHCVN_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtTHCVN.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

			Dim Result As String
			If KeyAscii = 13 Then
				If Find("ITC", "TCDSC", " Where TCTTC='" & txtTHTTC.Text & "' and TCEFR<>0 ") <> "" Then
					Result = Find("AVM", "VMNME", "WHERE VMVDN='" & txtTHCVN.Text & "'")
					If Result = "" Then
						cboTHCVN.Visible = True
						cboTHCVN.Focus()
					Else
						txtName.Text = Result
						If txtTHREF.Visible Then
							txtTHREF.Focus()
						Else
							txtTHQTY.Focus()
						End If

					End If
				Else
					Result = Find("RCM", "CMNME", "WHERE CMCNO='" & txtTHCVN.Text & "'")
					If Result = "" Then
						cboTHCVN.Visible = True
						cboTHCVN.Focus()
					Else
						txtName.Text = Result
						If txtTHREF.Visible Then
							txtTHREF.Focus()
						Else
							txtTHQTY.Focus()
						End If
					End If
				End If
			End If

			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtTHDOC_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtTHDOC.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim Result As Object
			If KeyAscii = 13 Then
				Result = Find("ITH", "THTID", "WHERE THDOC='" & txtTHDOC.Text & "'")
				If Result = "" Then
					txtTHDTE.Focus()
				Else
					txtTHTID.Text = Result
					MsgBox("Doc Number is already exist")
					NewTrans = False
					ISADDING = False
					ISEDITING = False
					WriteToScreen()
					ButtonStatus()
				End If
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtTHDTE_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtTHDTE.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then txtTHWHC.Focus()
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtTHITN_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtTHITN.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim sql As Object
			Dim DelStatus As Object
			Dim FieldDel As Object

			Dim Result As String
			If KeyAscii = 13 Then
				Result = Find("IIM", "IMITD", "WHERE IMITN='" & txtTHITN.Text & "'")

				If Result = "" Then
					cboTHITN.Visible = True
					cboTHITN.Focus()
				Else
					FieldDel = "LMRID"
					DelStatus = "LZ"
					sql = "select LMLOT from ILM where " & FieldDel & " <>'" & DelStatus & "'  AND LMITN='" & txtTHITN.Text & "' Group by LMLOT"
					Adodc7.ConnectionString = ConnectString
					Adodc7.ConnectionTimeout = 10
					Adodc7.CommandType = ADODB.CommandTypeEnum.adCmdText
					Adodc7.RecordSource = sql
					' MsgBox SQL
					Adodc7.Refresh()
					If txtTHREF.Visible Then 'Have PO
						Adodc6.RecordSource = "Select OLPON from POL Where OLITN='" & txtTHITN.Text & "' AND OLSTT <> '7' group by OLPON"
						Adodc6.Refresh()
					End If
					txtIMITD.Text = Result
					cboTHITN.Visible = False
					txtTHLTN.Focus()
				End If


			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub
		Sub OpenFile()
			Dim DelStatus As Object
			Dim FieldDel As Object

			Adodc1.ConnectionString = ConnectString
			Adodc1.ConnectionTimeout = 10
			Adodc1.CommandType = ADODB.CommandTypeEnum.adCmdText
			Adodc1.RecordSource = "SELECT ITH.*,RCM.CMNME As Name ,Users.Name as UserName, IIM.IMITD " & " FROM Users RIGHT OUTER JOIN ITH INNER JOIN " & " IIM ON ITH.THITN = IIM.IMITN LEFT OUTER JOIN " & " AVM ON ITH.THCVN = AVM.VMVDN LEFT OUTER JOIN " & " RCM ON ITH.THCVN = RCM.CMCNO ON " & " Users.UserName = ITH.THLUS" & " WHERE THDOC='" & txtTHDOC.Text & "' Order by THTID"
			Adodc1.Refresh()

			Adodc2.ConnectionString = ConnectString
			Adodc2.ConnectionTimeout = 10
			Adodc2.CommandType = ADODB.CommandTypeEnum.adCmdText
			Adodc2.RecordSource = "Select * From IWM where WHCID='" & COMPANYID & "'"
			Adodc2.Refresh()

			Adodc3.ConnectionString = ConnectString
			Adodc3.ConnectionTimeout = 10
			Adodc3.CommandType = ADODB.CommandTypeEnum.adCmdText
			FieldDel = "IMRID"
			DelStatus = "IZ"
			Adodc3.RecordSource = "select IMITN, IMITN + ' ' + IMITD as IMITD from IIM where " & FieldDel & " <>'" & DelStatus & "'"
			Adodc3.Refresh()

			Adodc4.ConnectionString = ConnectString
			Adodc4.ConnectionTimeout = 10
			Adodc4.CommandType = ADODB.CommandTypeEnum.adCmdText
			FieldDel = "CMRID"
			DelStatus = "CZ"
			Adodc4.RecordSource = "Select * from RCM where " & FieldDel & " <>'" & DelStatus & "'"
			Adodc4.Refresh()

			Adodc5.ConnectionString = ConnectString
			Adodc5.ConnectionTimeout = 10
			Adodc5.CommandType = ADODB.CommandTypeEnum.adCmdTable
			Adodc5.RecordSource = "ITC"
			Adodc5.Refresh()

			Adodc6.ConnectionString = ConnectString
			Adodc6.ConnectionTimeout = 10
			Adodc6.CommandType = ADODB.CommandTypeEnum.adCmdText
			Adodc6.RecordSource = "Select OLPON from POL group by OLPON"
			Adodc6.Refresh()


		End Sub

		Private Sub txtTHLTN_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtTHLTN.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim Response As Object
			Dim Result As String
			If KeyAscii = 13 Then
				Result = Find("ILM", "LMLOT", "WHERE LMLOT='" & txtTHLTN.Text & "'")

				If Result = "" Then
					Response = MsgBox("This LotNo doesn't exist.Do You want to add new LotNo?", MsgBoxStyle.YesNo + MsgBoxStyle.Critical + MsgBoxStyle.DefaultButton2, "Chu y'")
					If Response = MsgBoxResult.Yes Then
						Dim myform As New frmINV120
						myform.Show()
						'frmINV120/.ShowDialog()
					Else
						cboTHLTN.Visible = True

						If txtTHCVN.Visible Then
							txtTHCVN.Focus()
						Else
							txtTHQTY.Focus()
						End If
					End If
				End If
				If txtTHCVN.Visible Then
					txtTHCVN.Focus()
				Else
					txtTHQTY.Focus()
				End If
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtTHQTY_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtTHQTY.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then txtTHWGT.Focus()
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtTHREF_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtTHREF.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim Result As Object
			If KeyAscii = 13 Then
				If Find("ITC", "TCDSC", " Where TCTTC='" & txtTHTTC.Text & "' and TCEFR<>0") <> "" Then
					Result = Find("POL", "OLPON", "WHERE OLPON='" & txtTHREF.Text & "'")
					If Result = "" Then
						cboTHREF.Visible = True
						cboTHREF.Focus()
					Else
						txtTHREF.Text = Result
						txtTHQTY.Focus()
					End If
				Else
					txtTHREF.Text = ""
				End If

			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtTHTTC_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtTHTTC.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			Dim DelStatus As Object
			Dim FieldDel As Object

			Dim RecITC As ADODB.Recordset
			If KeyAscii = 13 Then
				txtTHTTC.Text = UCase(txtTHTTC.Text)
				RecITC = DBconn.Execute("Select * from ITC where TCTTC='" & txtTHTTC.Text & "'")
				If RecITC.EOF Then
					RecITC.Close()
					cboTHTTC.Visible = True
				Else
					txtTCDSC.Text = RecITC.Fields("TCDSC").Value
					If RecITC.Fields("TCEFR").Value Then
						Adodc4.ConnectionString = ConnectString
						Adodc4.ConnectionTimeout = 10
						Adodc4.CommandType = ADODB.CommandTypeEnum.adCmdText
						FieldDel = "VMRID"
						DelStatus = "VZ"
						Adodc4.RecordSource = "Select * from AVM where " & FieldDel & " <>'" & DelStatus & "'"
						Adodc4.Refresh()
						cboTHCVN.BoundColumn = "VMVDN"
						cboTHCVN.ListField = "VMNME"
						Adodc1.RecordSource = "SELECT ITH.*,AVM.VMNME As Name ,Users.Name as UserName, IIM.IMITD " & " FROM Users RIGHT OUTER JOIN ITH INNER JOIN " & " IIM ON ITH.THITN = IIM.IMITN LEFT OUTER JOIN " & " AVM ON ITH.THCVN = AVM.VMVDN LEFT OUTER JOIN " & " RCM ON ITH.THCVN = RCM.CMCNO ON " & " Users.UserName = ITH.THLUS" & " WHERE THDOC='" & txtTHDOC.Text & "' Order by THTID"
						Adodc1.Refresh()
					Else
						Adodc4.ConnectionString = ConnectString
						Adodc4.ConnectionTimeout = 10
						Adodc4.CommandType = ADODB.CommandTypeEnum.adCmdText
						FieldDel = "CMRID"
						DelStatus = "CZ"
						Adodc4.RecordSource = "Select * from RCM where " & FieldDel & " <>'" & DelStatus & "'"
						Adodc4.Refresh()
						Adodc4.Refresh()
						cboTHCVN.BoundColumn = "CMCNO"
						cboTHCVN.ListField = "CMNME"
						Adodc1.RecordSource = "SELECT ITH.*,RCM.CMNME As Name ,Users.Name as UserName, IIM.IMITD " & " FROM Users RIGHT OUTER JOIN ITH INNER JOIN " & " IIM ON ITH.THITN = IIM.IMITN LEFT OUTER JOIN " & " AVM ON ITH.THCVN = AVM.VMVDN LEFT OUTER JOIN " & " RCM ON ITH.THCVN = RCM.CMCNO ON " & " Users.UserName = ITH.THLUS" & " WHERE THDOC='" & txtTHDOC.Text & "' Order by THTID"
						Adodc1.Refresh()
					End If
					RecITC.Close()
					If txtTHITN.Enabled Then txtTHITN.Focus()
				End If

			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtTHWGT_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtTHWGT.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				If txtTHCST.Visible Then
					txtTHCST.Focus()
				Else
					txtTHREM.Focus()
				End If
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtTHWHC_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtTHWHC.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

			Dim Result As String
			If KeyAscii = 13 Then
				Result = Find("IWM", "WMDSC", "WHERE WMWHC='" & txtTHWHC.Text & "'")
				If Result = "" Then
					cboTHWHC.Visible = True
					cboTHWHC.Focus()
				Else
					txtWMDSC.Text = Result
					cboTHWHC.Visible = False
					txtTHTTC.Focus()
				End If

			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Sub ButtonStatus()

			cmdAddNew.Enabled = Not ISEDITING
			cmdReview.Enabled = Not ISEDITING
			cmdNewItem.Enabled = Not ISEDITING
			'cmdEdit.Enabled = Not ISEDITING
			cmdSave.Enabled = ISEDITING
			cmdCancel.Enabled = ISEDITING
			FrameTransaction.Enabled = NewTrans
			FrameItemTransactionDetail.Enabled = ISEDITING
		End Sub

		Sub WriteToFile()
			Dim RecEffect As ADODB.Recordset
			Dim WgtAdjust As Object
			Dim UntAdjust As Double


			RecEffect = DBconn.Execute("Select * from ITC where TCTTC='" & txtTHTTC.Text & "'")
			' Effect cost
			If RecEffect.Fields("TCEFC").Value Then
				WriteToFileIIM(txtTHITN, txtTHWGT, txtTHQTY, txtTHCST, "EffectCost")
			End If
			'If RecEffect!TCCYN Then
			'    WriteToFileIIM txtTHITN, txtTHWGT, txtTHQTY, txtTHCST, "CostEntry"
			'End If
			'///////////////////////////
			'EFFECT ON OPEN BALANCE
			'///////////////////////////
			Dim RecIIM As ADODB.Recordset
			If RecEffect.Fields("TCEFO").Value Then
				RecIIM = DBconn.Execute("Select * from IIM  Where IMITN='" & txtTHITN.Text & "'")
				With RecIIM
					WgtAdjust = Val(txtTHWGT.Text) - .Fields("IMOBW").Value
					UntAdjust = Val(txtTHQTY.Text) - .Fields("IMOBU").Value
					WriteToITH(WgtAdjust, UntAdjust)
					.Close()
				End With
				WriteToFileIIM(txtTHITN, WgtAdjust, UntAdjust, 0, "Opened")
				WriteToIWI(txtTHITN, txtTHWHC, WgtAdjust, UntAdjust, "Opened")
				WriteToILI(txtTHITN, txtTHWHC, txtTHLTN, WgtAdjust, UntAdjust, "Opened")
			Else
				WriteToITH(txtTHWGT, txtTHQTY)
			End If
			'EFFECT RECEIVED
			If RecEffect.Fields("TCEFR").Value Then
				WriteToFileIIM(txtTHITN, txtTHWGT, txtTHQTY, 0, "Recieved")
				WriteToIWI(txtTHITN, txtTHWHC, txtTHWGT, txtTHQTY, "Recieved")
				WriteToILI(txtTHITN, txtTHWHC, txtTHLTN, txtTHWGT, txtTHQTY, "Recieved")
				WriteToPOL(txtTHREF, txtTHITN, txtTHWGT, txtTHQTY)
			End If
			'EFFECT SOLD
			If RecEffect.Fields("TCEFS").Value Then
				WriteToFileIIM(txtTHITN, txtTHWGT, txtTHQTY, 0, "Sale")
				WriteToIWI(txtTHITN, txtTHWHC, txtTHWGT, txtTHQTY, "Sale")
				WriteToILI(txtTHITN, txtTHWHC, txtTHLTN, txtTHWGT, txtTHQTY, "Sale")
			End If
			'EFFECT ISSUED
			If RecEffect.Fields("TCEFI").Value Then
				WriteToFileIIM(txtTHITN, txtTHWGT, txtTHQTY, 0, "Issued")
				WriteToIWI(txtTHITN, txtTHWHC, txtTHWGT, txtTHQTY, "Issued")
				WriteToILI(txtTHITN, txtTHWHC, txtTHLTN, txtTHWGT, txtTHQTY, "Issued")
			End If
			'EFFECT ADJUST
			If RecEffect.Fields("TCEFA").Value Then
				WriteToFileIIM(txtTHITN, txtTHWGT, txtTHQTY, 0, "Adjusted")
				WriteToIWI(txtTHITN, txtTHWHC, txtTHWGT, txtTHQTY, "Adjusted")
				WriteToILI(txtTHITN, txtTHWHC, txtTHLTN, txtTHWGT, txtTHQTY, "Adjusted")
			End If
			' EFFECT ON ORDER
			If RecEffect.Fields("TCEFP").Value Then
				WriteToFileIIM(txtTHITN, txtTHWGT, txtTHQTY, 0, "OnOrder")
			End If


			RecEffect.Close()
			Adodc1.Refresh()
		End Sub
		Function ValidInput() As Object
			Dim StringErr As String
			StringErr = ""
			'Kiem tra txtTHTTC
			txtTHTTC.Text = UCase(txtTHTTC.Text)
			Dim RecITC As ADODB.Recordset
			RecITC = DBconn.Execute("Select * from ITC where TCTTC='" & txtTHTTC.Text & "'")
			Dim RecACN As ADODB.Recordset
			If RecITC.EOF Then
				StringErr = StringErr & "* Invalid Transaction code"
			Else
				'Kiem tra nhap Wgt and QTY
				txtTHQTY.Text = IIf(IsNumeric(txtTHQTY.Text), txtTHQTY.Text, 0)
				txtTHWGT.Text = IIf(IsNumeric(txtTHWGT.Text), txtTHWGT.Text, 0)
				If IsDBNull(txtTHQTY.Text) Or IsDBNull(txtTHWGT.Text) Then StringErr = StringErr & Chr(13) & "- Not yet input Weight or Quantity"
				'Kiem tra docNo
				If txtTHDOC.Text = "" Then StringErr = StringErr & Chr(13) & "- Not let Doc No blank"
				'Kiem tra Date
				RecACN = DBconn.Execute("Select * from ACN where CNTID='" & COMPANYTAX & "'")
				If RecACN.Fields("CNLCD").Value > CDate(txtTHDTE.Text) Then StringErr = StringErr & Chr(13) & "- Invalid date"
				'Kiem tra txtTHWHC
				If (Find("IWM", "WMDSC", "WHERE WMWHC='" & txtTHWHC.Text & "'") = "") Then StringErr = StringErr & Chr(13) & "- Invalid Warehouse code"
				'Kiem tra txtTHITN
				If Find("IIM", "IMITD", "WHERE IMITN='" & txtTHITN.Text & "'") = "" Then StringErr = StringErr & Chr(13) & "- Invalid Item Master code"
				'Kiemtra Lot bat buoc nhap neu IMLot=true
				If Find("IIM", "IMLOT", "WHERE IMITN='" & txtTHITN.Text & "'") <> 0 Then
					If Find("ILM", "LMLOT", "WHERE LMLOT='" & txtTHLTN.Text & "'") = "" Then StringErr = StringErr & Chr(13) & "- Invalid Lot No."
				Else
					If txtTHLTN.Text <> "" Then StringErr = StringErr & Chr(13) & "- Noneed input Lot No."
				End If
				'Kiem tra code Cust/Vendor
				If RecITC.Fields("TCEFR").Value And RecITC.Fields("TCEFC").Value Then
					If Find("AVM", "VMNME", "WHERE VMVDN='" & txtTHCVN.Text & "'") = "" Then StringErr = StringErr & Chr(13) & "- Invalid Vendor No."
					'Kiemtra so PO
					If Find("POL", "OLPON", "WHERE OLPON='" & txtTHREF.Text & "'") = "" Then
						StringErr = StringErr & Chr(13) & "- Invalid PO."
					Else
						If Find("POL", "OLITN", "WHERE OLPON='" & txtTHREF.Text & "' AND OLITN='" & txtTHITN.Text & "' AND OLRID<>'LZ'") = "" Then
							StringErr = StringErr & Chr(13) & "- This PO not order this Item."
						End If
					End If
				End If
				If RecITC.Fields("TCEFS").Value Then
					If Find("RCM", "CMNME", "WHERE CMCNO='" & txtTHCVN.Text & "'") = "" Then StringErr = StringErr & Chr(13) & "- Invalid Cust No."
				End If
				'Kiem tra nhap cost neu Effect costEntry<>0
				txtTHCST.Text = IIf(IsNumeric(txtTHCST.Text), txtTHCST.Text, 0)
				If RecITC.Fields("TCCYN").Value Then
					If Val(txtTHCST.Text) = 0 Then StringErr = StringErr & Chr(13) & "- Not yet input cost."
				End If
			End If
			If StringErr <> "" Then MsgBox(StringErr)
			ValidInput = (StringErr = "")
		End Function
	End Class
End Namespace